﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.IndexMarkServiceReference;
using SharedCode;
using System.Collections.ObjectModel;
using IndexMarkSilverlightClient.Classes;

namespace IndexMarkSilverlightClient.Views
{
    public partial class AddAnswerWindow : ChildWindow
    {
        public AddAnswerViewModel viewModel = null;

        public bool isUpdated = false;

        public AddAnswerWindow()
        {
            InitializeComponent();
            viewModel = Resources["AddAnswerViewModel"] as AddAnswerViewModel;
            viewModel.LoadCompleted += new ViewModelBase.LoadEventHandler(viewModel_LoadCompleted);
            this.Loaded += new RoutedEventHandler(AddAnswerWindow_Loaded);
        }

        void viewModel_LoadCompleted(object sender, LoadEventArgs e)
        {
            if (e.Type == typeof(Gradation))
            {
                SetOpacityControls(1);
                loadingBar.Visibility = Visibility.Collapsed;

                viewModel.FillGradationForNewAnswer();

                if (isUpdated)
                {
                    //когда загрузились градации, проставляем нужный RadioButton или вводим значение в NumerickUpDown
                    foreach (Gradation G in viewModel.Gradations)
                    {
                        foreach (Gradation Child in viewModel.Answer.Gradations)
                        {
                            if (G.Childs.Count == 0 && G.ID == Child.ID)
                            {
                                //делаем привязку к NumerickUpDown
                                G.Volume = Child.Volume;
                            }
                            else
                            {
                                Gradation FindChild = G.FindInChild(Child);
                                if (FindChild != null)
                                {
                                    FindChild.IsSelected = true;
                                    viewModel.AnswerChanged();
                                }
                            }
                        }
                    }
                }

            }
        }

        private void SetOpacityControls(double O)
        {
            foreach (UIElement ctrl in this.LayoutRoot.Children)
            {
                if (ctrl != loadingBar)
                {
                    ctrl.Opacity = O;
                }
            }
        }

        void AddAnswerWindow_Loaded(object sender, RoutedEventArgs e)
        {
            SetOpacityControls(0.6);
            viewModel.LoadRelationGradation();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (!isUpdated)
            {
                viewModel.Answer.State = StateObject.Created;
            }
            else
            {
                viewModel.Answer.State = StateObject.Updated;
            }
            viewModel.RemoveNullGradations();
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            Gradation SelectGrad = (sender as RadioButton).DataContext as Gradation;
            Gradation Parent = viewModel.GetParent(SelectGrad);
            int index = viewModel.Gradations.IndexOf(Parent);
            viewModel.Answer.Gradations[index] = SelectGrad;

            SelectGrad.IsSelected = (bool)(sender as RadioButton).IsChecked;
            viewModel.GradationsChanged();
            viewModel.AnswerChanged();

            //GradationsListBox.ItemsSource = viewModel.Gradations;
            //viewModel.AnswerChanged();
            //viewModel.GradationsChanged();
            //SelectGrad.PropertyChangedInit("IsCorrect");
        }

        private void NumericUpDown_ValueChanging(object sender, RoutedPropertyChangingEventArgs<double> e)
        {
            viewModel.Answer.Volume = (float?)(sender as NumericUpDown).Value;
            viewModel.AnswerChanged();
        }

        private void NumericUpDown_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            GradationChanged(sender);
        }

        private void GradationChanged(object sender)
        {
            Gradation SelectGrad = (sender as NumericUpDown).DataContext as Gradation;
            SelectGrad.Volume = (float?)(sender as NumericUpDown).Value;
            int index = viewModel.Gradations.IndexOf(SelectGrad);
            if (SelectGrad.Volume > 0) // только в случае если объем больше 0
            {
                viewModel.Answer.Gradations[index] = SelectGrad;
            }
            else
            {
                viewModel.Answer.Gradations[index] = new Gradation();
            }
            viewModel.AnswerChanged();
            viewModel.GradationsChanged();
        }

        private void NumericUpDown_KeyUp(object sender, KeyEventArgs e)
        {
            GradationChanged(sender);
        }

        private void NumericUpDown_KeyUp_1(object sender, KeyEventArgs e)
        {
            viewModel.Answer.Volume = (float?)(sender as NumericUpDown).Value;
            viewModel.AnswerChanged();
        }

    }
}

